<?php
/**
 * Created by PhpStorm.
 * User: 97606
 * Date: 2019/1/4
 * Time: 10:23
 */
namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\User\User;
use App\Modules\Log\Log;
use Illuminate\Http\Request;

class Login extends AdapterWebController
{
    // 管理员登录
    public function login(Request $request)
    {
        // get 页面
        if ($request->isMethod('get')) {
            return view('newAdmin.login.login');
        }
        // 提交数据
        $postData = $request->all();
        // 参数暂时放后台
        $postData['img_type'] = 'login';
        $postData['img_code_id'] = 1;
        $postData['auth_code'] = '';
        $postData['is_auto_login'] = 0;
        // post 提交
        $info = $this->appLayerExec(User::class, 'login', $postData);
        // 登录成功
        if ($info['code'] != 200) {
            return ['status' => -1, 'msg' => $info['msg']];
        }
        // 写入日志
        $logData['id'] = $info['res']['res']['id'];
        $logData['name'] = $postData['name'];
        $logData['type'] = 4;
        $this->appLayerExec(Log::class, 'addLog', $logData);
        // 写入session
        $request->session()->put('username', $postData['name']);
        $request->session()->put('userId', $info['res']['res']['id']);
        return [
            'status' => 0,
            'msg' => $info['msg'] . '，登陆成功！',
            'data' => ['id' => $info['res']['res']['id']]
        ];
    }

    // 管理员退出
    public function logout()
    {
        // 写入日志
        $logData['id'] = session('userId');
        $logData['name'] = session('username');
        $logData['type'] = 5;
        $this->appLayerExec(Log::class, 'addLog', $logData);
        // 注销session信息
        session()->forget('username');
        session()->forget('userId');
        session()->forget('request_token');
        return redirect('backstage/login');
    }
}
